import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { IRoute } from '../../types/route';
import { flattenRoute } from '../../utils/routes';

export interface AppState {
  routes: IRoute[];
  flattenRoutes: IRoute[];
  init: boolean;
}

let initialState: AppState = {
  routes: [],
  flattenRoutes: [], // 用户--获取的，真正平铺的路由
  init: false,
};

const appSlice = createSlice({
  name: 'app',
  initialState,
  reducers: {
    setSideBarRoutes(state, action: PayloadAction<IRoute[]>) {
      state.routes = action.payload;
      state.flattenRoutes = flattenRoute(action.payload, true, false);
      state.init = true;
    },
  },
});

export const { setSideBarRoutes } = appSlice.actions;

export default appSlice.reducer;
